package com.linghang.wusthelper.service.Impl;

import com.auth0.jwt.JWT;
import com.linghang.wusthelper.common.ServerResponse;
import com.linghang.wusthelper.dao.secondary.ActivityLDao;
import com.linghang.wusthelper.dao.secondary.ActivityMemberDao;
import com.linghang.wusthelper.dao.secondary.OrganizationDao;
import com.linghang.wusthelper.dao.secondary.VolunteerInfoDao;
import com.linghang.wusthelper.entity.secondary.ActivityL;
import com.linghang.wusthelper.entity.secondary.ActivityMember;
import com.linghang.wusthelper.entity.secondary.VolunteerInfo;
import com.linghang.wusthelper.service.VolunteerService;
import com.linghang.wusthelper.vo.secondary.ActivityInfoVo;
import com.linghang.wusthelper.vo.secondary.VolunteerInfoVo;
import org.aspectj.weaver.patterns.IToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @uthor Fegert
 * @Date 2020/3/1 19:17
 */
@Service
public class VolunteerServiceImpl implements VolunteerService {
    @Autowired
    private ActivityLDao activityLDao;
    @Autowired
    private VolunteerInfoDao volunteerInfoDao;
    @Autowired
    private OrganizationDao organizationDao;
    @Autowired
    private ActivityMemberDao activityMemberDao;

    @Override
    public ServerResponse getInfo(String Token) {
        String stuNum = JWT.decode(Token).getClaim("stuNum").asString();//token中获取stuNum
        VolunteerInfo volunteerInfo = volunteerInfoDao.selectBystuNum(stuNum);//查询志愿者信息
        ArrayList<ActivityMember> activityMembers = activityMemberDao.selectListByStuNum(stuNum);
        VolunteerInfoVo vo = null;
        if (volunteerInfo != null) {
            vo = toVolunteerInfoVo(volunteerInfo, activityMembers);
            return ServerResponse.createBySuccess("获取工时成功", vo);
        }
        return ServerResponse.createByError("获取工时失败（Case：用户不存在）", vo);
    }

    public VolunteerInfoVo toVolunteerInfoVo(VolunteerInfo volunteerInfo, ArrayList<ActivityMember>
            activityMembers) {
        VolunteerInfoVo vo = new VolunteerInfoVo();
        vo.setStudentNum(volunteerInfo.getStudentNum()); //设置学号
        vo.setName(volunteerInfo.getName());//设置姓名
        vo.setLevel(volunteerInfo.getLevel());//设置届别
        vo.setPhone(volunteerInfo.getPhone());//设置电话
        vo.setRoll(volunteerInfo.getRoll());
        vo.setVolunteerTime(volunteerInfo.getVolunteerTime());//设置总工时
        vo.setClassName(volunteerInfo.getClassName());//设置班级名称
        vo.setCollegeName(volunteerInfo.getCollegeName());//设置院级名称

        //添加活动详情
        for (ActivityMember activityMember : activityMembers) {
            ActivityInfoVo activityInfoVo = new ActivityInfoVo();
            activityInfoVo.setActivityId(activityMember.getActivityId());//设置活动id

            activityInfoVo.setVolunteerTime(activityMember.getVolunteerTime());//设置活动工时

            ActivityL activityL = activityLDao.selectByPrimaryKey(activityMember.getActivityId());//查询活动详情

            activityInfoVo.setActivityName(activityL.getName());//设置活动名称
            activityInfoVo.setTime(activityL.getTime());//设置活动时间

            String organizationName = organizationDao.selectNameById(activityL.getOrganizationId());//查询活动所属组织
            if (organizationName == null) {
                activityInfoVo.setOrganizationName("武汉科技大学青年志愿者服务总队");//设置活动所属组织
            } else {
                activityInfoVo.setOrganizationName(organizationName);
            }
            vo.getActivityList().add(activityInfoVo);

        }
        return vo;
    }

}
